package com.zkjd.lingdong.ui.home

import android.content.Context
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.adayo.service.utils.CarPropertyUtils
import com.zkjd.lingdong.data.FunctionsConfig
import com.zkjd.lingdong.model.ButtonType
import com.zkjd.lingdong.model.Device
import com.zkjd.lingdong.repository.DeviceEvent
import com.zkjd.lingdong.repository.DeviceRepository
import com.zkjd.lingdong.repository.SettingsRepository
import com.zkjd.lingdong.event.ButtonFunctionEvent
import com.wt.offline.utils.OfflineConfig
import com.zkjd.lingdong.bluetooth.BleAuthUtils
import com.zkjd.lingdong.bluetooth.BleConstants
import dagger.hilt.android.lifecycle.HiltViewModel
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import timber.log.Timber
import javax.inject.Inject

/**
 * 主页ViewModel
 */
@HiltViewModel
class HomeViewModel @Inject constructor(
    private val deviceRepository: DeviceRepository,
    private val buttonFunctionEvent: ButtonFunctionEvent,
    private val settingsRepository: SettingsRepository,
    @ApplicationContext private val context: Context,
) : ViewModel() {

    fun getContext(): Context = context
    internal val TAG = "BleManagerImpl"

    // 添加一个中间状态流以便我们可以处理从数据库获取的设备，并填充按键功能
    private val _devicesWithFunctions = MutableStateFlow<List<Device>>(emptyList())
    
    // 所有设备 - 使用中间状态流作为源
    val devices: StateFlow<List<Device>> = _devicesWithFunctions
        .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), emptyList())
    
    // 设备事件
    private val _uiEvents = MutableSharedFlow<UiEvent>()
    val uiEvents: SharedFlow<UiEvent> = _uiEvents
    
    // 是否使用镁佳车机执行器设置
    val useMeijiaCar: StateFlow<Boolean> = settingsRepository.useMeijiaCar
        .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), false)
        
    // 是否使用车机音频设置
    val useCarAudio: StateFlow<Boolean> = settingsRepository.useCarAudio
        .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), false)   
        
    // 是否启用设备鉴权设置
    val enableAuthentication: StateFlow<Boolean> = settingsRepository.enableAuthentication
        .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), true)

    // 是否启用设备鉴权设置
    val initialSetup: StateFlow<Boolean> = settingsRepository.initialSetup
        .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), true)

    // 是否启用设备鉴权设置
    val useMusic: StateFlow<Boolean> = settingsRepository.useMusic
        .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), true)
    // 音效选择
    val useMusicTypeName: StateFlow<String> = settingsRepository.useMusicTypeName
        .onEach { value ->Timber.w("读取DataStore22: $value") }
        .stateIn(viewModelScope, SharingStarted.Eagerly, "1")
        
    // 跟踪已成功连接的设备MAC地址
    private val connectedDevices = mutableSetOf<String>()

    init {
        if(!FunctionsConfig.getInstance(context).getIsMegaSys())
        {
            OfflineConfig.getInstance().registerListener {
                //服务已加载完成，可以读取配置
                OfflineConfig.getInstance().getValue("car_feature_back_door_type")
            }
        }

        // 监听设备事件
        viewModelScope.launch {
            deviceRepository.getDeviceEvents().collect { event ->
                when (event) {
                    is DeviceEvent.DeviceConnected -> {
                        _uiEvents.emit(UiEvent.ShowToast("设备已连接"))
                        // 记录设备已连接
                        connectedDevices.add(event.macAddress)
                    }
                    is DeviceEvent.DeviceDisconnected -> {
                        // 只有设备曾经成功连接过，才显示断开提示
                        if (connectedDevices.contains(event.macAddress)) {
                            _uiEvents.emit(UiEvent.ShowToast("设备已断开连接"))
                            // 从已连接设备列表中移除
                            connectedDevices.remove(event.macAddress)
                        }
                    }
                    is DeviceEvent.ConnectionFailed -> {
                        _uiEvents.emit(UiEvent.ShowToast("连接失败: ${event.reason}"))
                    }
                    is DeviceEvent.ButtonPressed -> {
                        // 显示按键功能名称或按键类型
                        handleButtonPressed(event)
                    }
                    is DeviceEvent.BatteryLevelChanged -> {
                        // 电量变化事件不需要UI反馈
                        if(event.level<=10)
                            _uiEvents.emit(UiEvent.Navigate(event.macAddress))
                    }
                    is DeviceEvent.DeviceReady->{
                        // 连接已准备好
                    }
                    is DeviceEvent.AuthSuccess -> {
                        _uiEvents.emit(UiEvent.ShowToast("设备鉴权成功"))
                    }
                    is DeviceEvent.AuthFailed -> {
                        //_uiEvents.emit(UiEvent.ShowToast("设备鉴权失败: ${event.reason}"))
                    }

                    else -> {}
                }
            }
        }

//        // 监听Function事件
//        viewModelScope.launch {
//            carFunctionExecutor.getDeviceEvents().collect { event ->
//                when(event){
//                    is DeviceEvent.SetFunction -> {
//                        // 如果有配置功能，显示功能名称；否则显示按键类型
//                        val message ="${event.keyString}"
//                        // 从当前内存中的设备列表获取设备信息，避免额外的数据库查询
////                        val device = _devicesWithFunctions.value.find { it.macAddress == event.macAddress }
////                        if (device == null) {
////                            _uiEvents.emit(UiEvent.ShowToast("未知设备: ${event.macAddress}"))
////                            return@launch
////                        }
//                        _uiEvents.emit(UiEvent.ShowToast(message))
//                    }
//                    else -> {}
//                }
//            }
//        }
//
//        // 监听Function事件
//        viewModelScope.launch {
//            carMeiJiaFunctionExecutor.getDeviceEvents().collect { event ->
//                when(event){
//                    is DeviceEvent.SetFunction -> {
//                        // 如果有配置功能，显示功能名称；否则显示按键类型
//                        val message ="${event.keyString}"
//                        // 从当前内存中的设备列表获取设备信息，避免额外的数据库查询
////                        val device = _devicesWithFunctions.value.find { it.macAddress == event.macAddress }
////                        if (device == null) {
////                            _uiEvents.emit(UiEvent.ShowToast("未知设备: ${event.macAddress}"))
////                            return@launch
////                        }
//                        _uiEvents.emit(UiEvent.ShowToast(message))
//                    }
//                    else -> {}
//                }
//            }
//        }
        
        // 监听设备列表变化
        viewModelScope.launch {
            deviceRepository.getAllDevices().collect { deviceList ->
                _devicesWithFunctions.value = deviceList
            }
        }
        
        // 监听按键功能变更事件
        viewModelScope.launch {
            buttonFunctionEvent.functionChangedEvent.collect { macAddress ->
                refreshDeviceFunctions(macAddress)
            }
        }
    //模拟鉴权测试使用
//        val car=BleAuthUtils.hexStringToByteArray("1122334455667788")
//        val lingdo=BleAuthUtils.hexStringToByteArray("1122334455667788")
//        val encryptedAddr=BleAuthUtils.hexStringToByteArray("53F778B74BB277A3751DE2919BDEFC47")
//        val authData=BleAuthUtils.hexStringToByteArray("EDEC92B40B780E54414A2D3BE8CD86E6")
//
//        val device="DE:3A:19:16:18:39"
//
//        Timber.tag(com.zkjd.lingdong.bluetooth.TAG).e("车机随机=1122334455667788")
//        Timber.tag(com.zkjd.lingdong.bluetooth.TAG).e("灵动随机=1122334455667788")
//        Timber.tag(com.zkjd.lingdong.bluetooth.TAG).e("需解地址=53F778B74BB277A3751DE2919BDEFC47")
//        Timber.tag(com.zkjd.lingdong.bluetooth.TAG).e("认证数据=EDEC92B40B780E54414A2D3BE8CD86E6")
//        // 生成密钥材料
//        val (iv, key) = BleAuthUtils.generateKeyMaterial(
//            car,
//            lingdo,
//            BleConstants.Auth.AUTH_LABEL
//        )
//
//        Timber.tag(com.zkjd.lingdong.bluetooth.TAG).e("iv=${BleAuthUtils.byteArrayToHexString(iv)}")
//        Timber.tag(com.zkjd.lingdong.bluetooth.TAG).e("key=${BleAuthUtils.byteArrayToHexString(key)}")
//
//        val expectedAddr = BleAuthUtils.padDeviceName2(device.replace(":",""), BleConstants.Auth.PUBLIC_ADDR_LENGTH)
//        val decryptedAddr = BleAuthUtils.aesDecrypt(encryptedAddr, key, iv)
//        Timber.tag(com.zkjd.lingdong.bluetooth.TAG).e("解密=${BleAuthUtils.byteArrayToHexString(decryptedAddr)}")
//        Timber.tag(com.zkjd.lingdong.bluetooth.TAG).e("我的=${BleAuthUtils.byteArrayToHexString(expectedAddr.reversedArray())}")
//
//        // 检查PublicAddr是否匹配
//        val addrMatched = decryptedAddr.contentEquals(expectedAddr.reversedArray())
//        if (!addrMatched) {
//            Timber.tag(com.zkjd.lingdong.bluetooth.TAG).e("PublicAddr不匹配: 期望=${BleAuthUtils.byteArrayToHexString(expectedAddr)}, 实际=${BleAuthUtils.byteArrayToHexString(decryptedAddr)}")
//        }
//
//        val expectedRawData = BleAuthUtils.generateRawAuthData(car, lingdo).copyOfRange(0,BleConstants.Auth.PUBLIC_ADDR_LENGTH).reversedArray()
//        val decryptedAuthData = BleAuthUtils.aesDecrypt(authData, key, iv)
//
//        Timber.tag(com.zkjd.lingdong.bluetooth.TAG).e("解密认证=${BleAuthUtils.byteArrayToHexString(decryptedAuthData)}")
//        Timber.tag(com.zkjd.lingdong.bluetooth.TAG).e("我的认证=${BleAuthUtils.byteArrayToHexString(expectedRawData)}")
    }

    /**
     * 保存选择的车辆类型名称
     */
    fun toSaveChoseCar(id: String) {
        viewModelScope.launch {
            //settingsRepository.setUseCarTypeName(id)
            settingsRepository.setUseMusicTypeName(id)
        }
    }

    /**
     * 切换车机执行器类型
     */
    fun toggleCarExecutor() {
        viewModelScope.launch {
            val currentValue = useMeijiaCar.value
            if(!currentValue) {
                settingsRepository.setUseMeijiaCar(!currentValue)
                val currentValue2 = useCarAudio.value
                settingsRepository.setUseCarAudio(!currentValue2)
                _uiEvents.emit(UiEvent.ShowToast("已切换到${if (!currentValue) "镁佳Car" else "CarService"}"))
            }
        }
    }


    /**
     * 切换车机音频类型
     */
    fun toggleCarAudio() {
        viewModelScope.launch {
            val currentValue = useCarAudio.value
            if(!currentValue) {
                settingsRepository.setUseCarAudio(!currentValue)
                val currentValue2 = useMeijiaCar.value
                settingsRepository.setUseMeijiaCar(!currentValue2)
                _uiEvents.emit(UiEvent.ShowToast("已切换到${if (!currentValue) "CarAudio" else "AudioManager"}"))
            }
        }
    }
    
    /**
     * 切换设备鉴权开关
     */
    fun toggleAuthentication() {
        viewModelScope.launch {
            val currentValue = enableAuthentication.value
            settingsRepository.setEnableAuthentication(!currentValue)
            _uiEvents.emit(UiEvent.ShowToast("设备鉴权已${if (!currentValue) "开启" else "关闭"}"))
        }
    }

    /**
     * 切换设备鉴权开关
     */
    fun toggleMusic() {
        viewModelScope.launch {
            val currentValue = useMusic.value
            settingsRepository.setUseMusic(!currentValue)
            _uiEvents.emit(UiEvent.ShowToast("音效已${if (!currentValue) "开启" else "关闭"}"))
        }
    }

    /**
     * 首次弹出已完成，后续无需再弹出
     */
    fun toInitialSetupExecutor() {
        viewModelScope.launch {

            settingsRepository.setInitialSetup(true)

        }
    }


    /**
     * 刷新指定设备的按键功能
     */
    private suspend fun refreshDeviceFunctions(macAddress: String) {
        val currentDevices = _devicesWithFunctions.value
        val deviceIndex = currentDevices.indexOfFirst { it.macAddress == macAddress }
        
        if (deviceIndex >= 0) {
            val device = currentDevices[deviceIndex]
            
            // 为设备加载所有类型的按键功能
            val shortPressFunction = deviceRepository.getButtonFunction(device.macAddress, ButtonType.SHORT_PRESS)
            val longPressFunction = deviceRepository.getButtonFunction(device.macAddress, ButtonType.LONG_PRESS)
            val doubleClickFunction = deviceRepository.getButtonFunction(device.macAddress, ButtonType.DOUBLE_CLICK)
            val leftRotateFunction = deviceRepository.getButtonFunction(device.macAddress, ButtonType.LEFT_ROTATE)
            val rightRotateFunction = deviceRepository.getButtonFunction(device.macAddress, ButtonType.RIGHT_ROTATE)
            
            // 创建包含更新后按键功能的新设备对象
            val updatedDevice = device.copy(
                shortPressFunction = shortPressFunction,
                longPressFunction = longPressFunction,
                doubleClickFunction = doubleClickFunction,
                leftRotateFunction = leftRotateFunction,
                rightRotateFunction = rightRotateFunction
            )
            
            // 更新设备列表
            val updatedDevices = currentDevices.toMutableList()
            updatedDevices[deviceIndex] = updatedDevice
            _devicesWithFunctions.value = updatedDevices
            
            Timber.d("已更新设备按键功能: ${device.macAddress}")
        }
    }
    
    /**
     * 加载设备的所有按键功能并更新设备列表
     */
    private fun loadDeviceFunctions(deviceList: List<Device>) {
        viewModelScope.launch {
            val updatedDevices = deviceList.map { device ->
                // 为每个设备加载所有类型的按键功能
                val shortPressFunction = deviceRepository.getButtonFunction(device.macAddress, ButtonType.SHORT_PRESS)
                val longPressFunction = deviceRepository.getButtonFunction(device.macAddress, ButtonType.LONG_PRESS)
                val doubleClickFunction = deviceRepository.getButtonFunction(device.macAddress, ButtonType.DOUBLE_CLICK)
                val leftRotateFunction = deviceRepository.getButtonFunction(device.macAddress, ButtonType.LEFT_ROTATE)
                val rightRotateFunction = deviceRepository.getButtonFunction(device.macAddress, ButtonType.RIGHT_ROTATE)
                
                // 创建包含按键功能的新设备对象
                device.copy(
                    shortPressFunction = shortPressFunction,
                    longPressFunction = longPressFunction,
                    doubleClickFunction = doubleClickFunction,
                    leftRotateFunction = leftRotateFunction,
                    rightRotateFunction = rightRotateFunction
                )
            }
            
            Timber.d("已加载设备按键功能: ${updatedDevices.map { it.macAddress to listOf(
                it.shortPressFunction != null,
                it.longPressFunction != null,
                it.doubleClickFunction != null,
                it.leftRotateFunction != null,
                it.rightRotateFunction != null
            ) }}")
            
            _devicesWithFunctions.value = updatedDevices
        }
    }

    /**
     * 删除设备
     */
    fun deleteDevice(device: Device) {
        viewModelScope.launch {
            try {
                withContext(Dispatchers.IO) {
                    // 删除设备同时会尝试取消配对
                    deviceRepository.deleteDevice(device)
                }
                // 发送成功提示
                _uiEvents.emit(UiEvent.ShowToast("设备已删除：${device.name}"))
            } catch (e: Exception) {
                Timber.e(e, "删除设备失败: ${device.macAddress}")
                _uiEvents.emit(UiEvent.ShowToast("删除设备失败：${e.message}"))
            }
        }
    }

    /**
     * 重命名设备
     */
    fun renameDevice(macAddress: String, name: String) {
        viewModelScope.launch {
            withContext(Dispatchers.IO) {
                deviceRepository.renameDevice(macAddress, name)
            }
        }
    }

    /**
     * 刷新设备信息
     */
    fun refreshDevices() {
        viewModelScope.launch {
            withContext(Dispatchers.IO) {
                deviceRepository.refreshDevices();
            }
        }
    }

    /**
     * 处理按钮按下事件
     * 显示功能名称或按键类型
     */
    private fun handleButtonPressed(event: DeviceEvent.ButtonPressed) {
        viewModelScope.launch {
            try {
                // 从当前内存中的设备列表获取设备信息，避免额外的数据库查询
                val device = _devicesWithFunctions.value.find { it.macAddress == event.macAddress }
                if (device == null) {
                    _uiEvents.emit(UiEvent.ShowToast("未知设备: ${event.macAddress}"))
                    return@launch
                }
                
                // 根据按键类型获取对应的功能
                val buttonFunction = when (event.buttonType) {
                    ButtonType.SHORT_PRESS -> device.shortPressFunction
                    ButtonType.LONG_PRESS -> device.longPressFunction
                    ButtonType.DOUBLE_CLICK -> device.doubleClickFunction
                    ButtonType.LEFT_ROTATE -> device.leftRotateFunction
                    ButtonType.RIGHT_ROTATE -> device.leftRotateFunction //right是空的，旋转就存一个leftRotateFunction
                    ButtonType.FONE_PESS ->  null
                    ButtonType.FTWO_PESS ->  null
                }
                
                // 如果有配置功能，显示功能名称；否则显示按键类型
                val message = if (buttonFunction != null) {
                    "执行功能: ${buttonFunction.name}"
                } else {

                    when(event.buttonType)
                    {
                        ButtonType.FONE_PESS -> "进入返控模式"
                        ButtonType.FTWO_PESS -> "退出返控模式"
                        else -> "按键事件: ${getButtonTypeDisplayName(event.buttonType)}"
                    }

                }

                if (buttonFunction == null) {
                    _uiEvents.emit(UiEvent.ShowToast(device.name+message))
                }
                

                Timber.d("按键事件: ${device.name}, ${event.buttonType}, ${buttonFunction?.name ?: "未配置功能"}")
            } catch (e: Exception) {
                Timber.e(e, "处理按键事件失败: ${event.macAddress}, ${event.buttonType}")
            }
        }
    }

    /**
     * 获取按键类型的显示名称
     */
    private fun getButtonTypeDisplayName(buttonType: ButtonType): String {
        return when (buttonType) {
            ButtonType.SHORT_PRESS -> "短按"
            ButtonType.LONG_PRESS -> "长按"
            ButtonType.DOUBLE_CLICK -> "双击"
            ButtonType.LEFT_ROTATE -> "左旋转"
            ButtonType.RIGHT_ROTATE -> "右旋转"
            else -> ""
        }
    }



    /**
     * UI事件
     */
    sealed class UiEvent {
        data class ShowToast(val message: String) : UiEvent()
        data class Navigate(val route: String) : UiEvent()
    }
} 